package library.and.xps.com.step.view.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;

import com.github.mikephil.charting.data.Entry;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import library.and.xps.com.step.R;
import library.and.xps.com.step.util.StepGenerator;
import library.and.xps.com.step.m.model.DaliyRecord;
import library.and.xps.com.step.m.model.WeeklyRecord;
import library.and.xps.com.step.util.CommonUtils;
import library.and.xps.com.step.util.DateUtil;
import library.and.xps.com.step.view.fragment.HistoryFragment;

public class HistoryActivity extends AppCompatActivity implements View.OnClickListener {
    Toolbar toolbar;
    CheckBox tv_daily, tv_weekly, tv_monthly;
    HistoryFragment fragmentBarChat;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hostory);
        tv_daily = (CheckBox) findViewById(R.id.tv_daily);
        tv_daily.setOnClickListener(this);
        tv_weekly = (CheckBox) findViewById(R.id.tv_weekly);
        tv_weekly.setOnClickListener(this);
        tv_monthly = (CheckBox) findViewById(R.id.tv_monthly);
        tv_monthly.setOnClickListener(this);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("StepLab!");
        toolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));

        setSupportActionBar(toolbar);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);
        actionBar.setHomeAsUpIndicator(R.mipmap.back);

        EventBus.getDefault().register(this);
        fragmentBarChat = new HistoryFragment();
        CommonUtils.replaceFragment(R.id.frag_container_history, fragmentBarChat, getSupportFragmentManager());

        tv_daily.setTextColor(Color.parseColor("#ffffff"));
        tv_daily.setChecked(true);
        fragmentBarChat.toogleDaily();

        if(!(StepGenerator.getInstance().getRecordAll().size()>=7))//当记录小于7条时 禁用周视图
            tv_weekly.setEnabled(false);

    }

    //在图表被点击的时候调用
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void PostThread(Entry e) {
        Object o = e.getData();
        if (o instanceof DaliyRecord) {
            DaliyRecord daliyRecord = (DaliyRecord) o;
            if (daliyRecord.getStepCount() != 0) {
                toolbar.setTitle(DateUtil.formatDate(daliyRecord.getTimeStamp()));
            } else {
                toolbar.setTitle("StepLab!");
            }
        } else if (o instanceof WeeklyRecord) {
            WeeklyRecord weekRecord = (WeeklyRecord) o;
            if (weekRecord.getStepsAverage() != 0) {
                toolbar.setTitle(weekRecord.getDateSpanDes());
            } else {
                toolbar.setTitle("StepLab!");
            }
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case  android.R.id.home:
                finish();
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    void resetCheckBt() {
        toolbar.setTitle("StepLab!");
        tv_daily.setChecked(false);
        tv_monthly.setChecked(false);
        tv_weekly.setChecked(false);
        tv_weekly.setTextColor(Color.parseColor("#707070"));
        tv_monthly.setTextColor(Color.parseColor("#707070"));
        tv_daily.setTextColor(Color.parseColor("#707070"));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_daily:
                resetCheckBt();
                fragmentBarChat.toogleDaily();
                tv_daily.setTextColor(Color.parseColor("#ffffff"));
                tv_daily.setChecked(true);
                break;
            case R.id.tv_weekly:
                resetCheckBt();
                fragmentBarChat.toogleWeekly();
                tv_weekly.setTextColor(Color.parseColor("#ffffff"));
                tv_weekly.setChecked(true);
                break;
            case R.id.tv_monthly:
                resetCheckBt();
                fragmentBarChat.toogleMonthly();
                tv_monthly.setTextColor(Color.parseColor("#ffffff"));
                tv_monthly.setChecked(true);
                break;
        }
    }
}
